<?php

/**
 * 订单控制器
 *
 * @author Alen
 * @since 1.0 2014-10-10 by sutroon
 */
class OrderAction extends BaseAction {

    public function __construct() {
        parent::__construct();
        $this->UPR = $this->getUPR();
    }

    /**
     * 获取用户权限规则数据
     * @since 1.0 2014-12-30 by sutroon
     */
    function getUPR() {
        $uid = $this->user_login_data['id'];

        $upr = array();
        $upr['订单管理'] = equal_permission('DD_DDGL', $uid);
        $upr['新增订单'] = equal_permission('DD_XZDD', $uid);
        $upr['编辑订单'] = equal_permission('DD_BJDD', $uid);
        $upr['删除订单'] = true; // equal_permission('DD_SCDD', $uid);
        $upr['取消订单'] = true; // equal_permission('DD_QXDD', $uid);
        $upr['审核订单'] = true; // equal_permission('DD_SHDD', $uid);
        $upr['仓库发货'] = true; // equal_permission('DD_CKFH', $uid);
        $upr['订单结算'] = true; // equal_permission('DD_DDJS', $uid);
        $upr['导出订单数据'] = true; // equal_permission('DD_DCDDSJ', $uid);
        $upr['订单列表标签-全部订单'] = true;
        $upr['订单列表标签-审核退回'] = true;
        $upr['订单列表标签-待审核'] = true;
        $upr['订单列表标签-待发货'] = true;
        $upr['订单列表标签-已发货'] = true;
        $upr['订单列表标签-已签收'] = true;
        $upr['订单列表标签-拒签'] = true;
        $upr['订单列表标签-已退货'] = true;
        $upr['订单列表标签-已取消'] = true;
        $upr['订单列表标签-已结算'] = true;
        $upr['订单列表标签-未结算'] = true;
        $upr['订单列表标签-调度订单'] = true;
        return $upr;
    }

    /**
     * 获取订单列表搜索数据
     * @return array
     * @since 1.0 2015-1-8 by sutroon
     */
    function order_list_get_search() {
        $tab = I('tab');
        $search['tel'] = I('tel');
        $search['name'] = I('name');
        $search['sn'] = I('sn');
        $search['sellerUserName'] = I('sellerUserName');
        $search['orderAmountMin'] = I('orderAmountMin');
        $search['orderAmountMax'] = I('orderAmountMax');
        if ($tab) {
            $search['tab'] = $tab;
        }
        if ($search['tel']) {
            $where['telphone'] = $search['tel'];
        }
        if ($search['name']) {
            $where['personalName'] = $search['name'];
        }
        if ($search['sn']) {
            $where['serialNo'] = $search['sn'];
        }
        if ($search['orderAmountMin']) {
            $where['orderAmount'][] = array('EGT', $search['orderAmountMin']);
        }
        if ($search['orderAmountMax']) {
            $where['orderAmount'][] = array('ELT', $search['orderAmountMax']);
        }
        if ($search['sellerUserName']) {
            $where['sellerUserName'] = $search['sellerUserName'];
        }
        if ($this->RoleInfo['SEAT']) {
            $where['sellerID'] = $this->user_login_data['id'];
        } else if ($this->RoleInfo['ADMIN']) {
            $where['siteID'] = $this->user_login_data['siteID'];
        }
        $tpl = '';
        $table = 'vorder';
        switch ($tab) {
            case 'unconfirmed': // 待审核(状态:0=未审核)
                $tpl = 'order_list_unconfirmed_part';
                $where['state'] = 0;
                break;
            case 'returnconfirm': // 审核退回(状态:24=核单失败,34=仓库退回)
                $tpl = 'order_list_returnconfirm_part';
                $where['state'] = array('IN', '24,34');
                break;
            case 'unfilled': // 待发货(状态:20=核单通过)
                $tpl = 'order_list_unfilled_part';
                $where['state'] = 20;
                break;
            case 'delivered': // 已发货(状态:30=已发货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_delivered_part';
                $where['state'] = 30;
                break;
            case 'signed': // 客户签收(状态:50=已签收)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_part';
                $where['state'] = 50;
                break;
            case 'refused': // 拒绝签收(状态:54=客户拒签)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_part';
                $where['state'] = 54;
                break;
            case 'returned': // 已退货(状态:55=客户退货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_returned_part';
                $where['state'] = 55;
                break;
            case 'canceled': // 已取消(40=座席取消,41=买家取消,42=逾期取消,43=订单无效取消)
                $tpl = 'order_list_canceled_part';
                $where['state'] = array('BETWEEN', '40,49');
                break;
            case 'settled': // 已结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_part';
                $where['orderSettlementState'] = array('GT', 0);
                break;
            case 'unsettled': // 未结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_part';
                $where['orderSettlementState'] = 0;
                break;
            case 'dispatch': // 调度订单
                $tpl = 'order_list_dispatch_part';
                $where['oldSellerID'] = array('GT', 0);
                break;
        }
        return array('table' => $table, 'search' => $search, 'where' => $where, 'tpl' => $tpl);
    }

    // 订单列表 2014-1-8 by sutroon
    public function order_list() {
        $arrSearch = $this->order_list_get_search();
        if (I('action') == 'export') {
            $this->checkUPR('导出订单数据');
            $M = new SoViewModel($arrSearch['table']);
            $list = $M->field("personalName as '客户姓名', telphone as '电话号码', address as '联系地址', serialNo as '客户编号', buyCount as '购买次数', cumulativeAmount as '累计消费', lastBuyTime as '上次购买时间', userName as '工号', createdTime as '创建时间'")->where($arrSearch['where'])->select();
            if (!$list) {
                $this->error('找不到符合条件的记录!');
            }
            sofunc_excel_export($list);
        } else {
            $this->checkUPR('订单管理');
            $list = sofunc_get_paging_list($arrSearch['table'], $arrSearch['where'], $show, array('ORDERBY' => 'createdTime desc', 'PAGEPARAMS' => $arrSearch['search']));
            $this->assign('list', set_siteID_text($list));
            $this->assign('page', $show);
            $this->assign('search', $arrSearch['search']);
            $this->assign('UPR', $this->UPR);
            $this->display2($arrSearch['tpl']);
        }
    }

    // 查看指定客户编号的所有订单
    public function customer_order_list() {
        $buyerID = I('buyerID');
        if (!$buyerID) {
            $this->act_close_dialog(false, '参数有误!', '', false);
        }
        $where['buyerID'] = $buyerID;
        if (!$this->RoleInfo['SUPERADMIN']) {
            $where['siteID'] = $this->user_login_data['siteID'];
        }
        $this->assign('UPR', $this->UPR);
        $this->itemList('torder', $where);
    }

    // 添加订单界面
    public function order_add() {
        $this->checkUPR('新增订单');
        $buyerID = I('buyerID');
        if (!$buyerID) {
            $this->act_close_dialog(false, '参数有误!');
        }
        $data = M('tcustomer')->field('personalName as consigneeName, address as consigneeAddress, telphone as consigneeTelphone')->find($buyerID);
        $def_data = array('serialNo' => get_serialNo('DD', $this->user_login_data['siteID'], $this->user_login_data['id']), 'siteID' => $this->user_login_data['siteID'], 'buyerID' => $buyerID, 'sellerID' => $this->user_login_data['id']);
        $this->assign('data', $data ? ($def_data + $data) : $def_data);
        $this->assign('UPR', $this->UPR);

        $this->display();
    }

    // 编辑订单界面
    public function order_edit() {
        $this->checkUPR('编辑订单');
        $id = I('id');
        if (!$id) {
            $this->act_close_dialog(false, '参数有误!');
        }
        $data = M('torder')->find($id);
        if (!$data) {
            $this->act_close_dialog(false, '该订单不存在!');
        }
        $this->assign('data', $data);
        $this->assign('UPR', $this->UPR);
        $this->display();
    }

    // 编辑订单数据保存
    public function order_edit_save() {
        $result = $this->order_edit_save_func();
        if (false !== $result) {
            $this->act_close_dialog(true, '保存成功!');
        } else {
            $this->act_error('保存失败!');
        }
    }

    // 编辑订单数据保存方法 2015-1-12 by sutroon
    function order_edit_save_func() {
        $id = I('id');
        $this->checkUPR($id ? '编辑订单' : '新增订单');
        $params = create_params();
        $params->addParam('siteID', 'require', '租户ID无效');
        $params->addParam('serialNo', 'require', '订单编号无效');
        $params->addParam('consigneeName', 'require', '客户姓名未填写');
        $params->addParam('consigneeTelphone', 'require', '联系电话未填写');
        $params->addParam('consigneeAddress', 'require', '收货地址未填写');
        $params->addParam('buyerID', 'require', '客户编号无效');
        $params->addParam('sellerID', 'require', '座席编号无效');
        if ($params->hasError()) {
            $this->act_error($params->getError());
        }
        $params->addData('consigneeZip');
        if (I('sellerRemark')) {
            $params->addData('sellerRemark');
        }
        if (I('buyerRemark')) {
            $params->addData('buyerRemark');
        }
        if (I('confirmRemark')) {
            $params->addData('confirmRemark');
        }
        if (I('storekeeperRemark')) {
            $params->addData('storekeeperRemark');
        }

        $buyerID = I('buyerID');
        $sellerID = I('sellerID');
        $serialNo = I('serialNo');
        // 保存购物车数据
        $result = OrderItemsAction::save_to_orderitems($buyerID, $sellerID, $serialNo, $id);
        if (!$result['success']) {
            $this->act_error($result['message']);
        }
        $params->addData('itemCount', $result['message']['count']);
        $params->addData('productAmount', $result['message']['amount']);
        $params->addData('orderAmount', $result['message']['amount']);
        $params->addData('deliveryFee', 0);
        $params->addData('paymentID', 0);
        $params->addData('isNeedInvoice', 0);
        $M = M('torder');
        if ($id) {
            $params->addData('id');
            $result = $M->save($params->getParams());
        } else {
            $result = $M->add($params->getParams());
            if (false !== $result) {
                $M->execute("update torder_items set orderID=$result where orderID=0 and sellerID=$sellerID;");
            }
        }
        return $result;
    }

    // 审核订单 2015-1-12 by sutroon
    public function order_confirm() {
        $id = I('id');
        if (!$id) {
            $this->act_close_dialog(false, '参数有误!');
        }
        $data = M('torder')->find($id);
        $this->assign('data', $data);
        $this->assign('UPR', $this->UPR);
        $this->display();
    }

    // 审核订单数据保存 2015-1-12 by sutroon
    public function order_confirm_save() {
        $id = I('id');
        $action = I('action');
        if (!$id || !$action) {
            $this->act_close_dialog(false, '参数有误!');
        }
        switch ($action) {
            case 'success':
                $result = $this->order_edit_save_func();
                if (false !== $result) {
                    M('torder')->where("id=$id")->setField('state', 20);
                }
                break;
            case 'return':
                M('torder')->where("id=$id")->setField('state', 24);
                break;
            default:
                $result = $this->order_edit_save_func();
                break;
        }
        if (false !== $result) {
            $this->act_close_dialog(true, '操作成功!');
        } else {
            $this->act_error('操作失败!');
        }
    }


    // 发货员审核 2015-1-12 by sutroon
    public function order_delivery() {
        $id = I('id');
        if (!$id) {
            $this->act_close_dialog(false, '参数有误!');
        }
        $data = M('torder')->find($id);
        $this->assign('data', $data);
        $this->assign('UPR', $this->UPR);
        $this->display();
    }

    // 发货员审核订单数据保存 2015-1-12 by sutroon
    public function order_delivery_save() {
        $id = I('id');
        $action = I('action');
        if (!$id || !$action) {
            $this->act_close_dialog(false, '参数有误!');
        }
        switch ($action) {
            case 'success':
                $result = $this->order_edit_save_func();
                if (false !== $result) {
                    M('torder')->where("id=$id")->setField('state', 20);
                }
                break;
            case 'return':
                M('torder')->where("id=$id")->setField('state', 24);
                break;
            default:
                $result = $this->order_edit_save_func();
                break;
        }
        if (false !== $result) {
            $this->act_close_dialog(true, '操作成功!');
        } else {
            $this->act_error('操作失败!');
        }
    }
}
